#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#I, hereby disclaims all copyright interest in the program
#Financial Model written by Xingzhong Xu for FE-621A at 
#Stevens Institute of Technology, NJ.
#
#Xingzhong, 18 September, 2011
#xxu7@stevens.edu

import numpy as np
import matplotlib.pyplot as plt

class lto:
    def __init__(self, mu=0, sigma2 = 1, s = 100, dt = 1):
        self.mu = mu
        self.sigma2 = sigma2
        self.s = s
        self.dt = dt
        self.S = [s]

    def next(self, rand):
        dss = self.mu * self.dt + self.sigma2 * rand * np.sqrt(self.dt)
        ds = self.s * ( 1 + dss )
        return ds
    
    def gen(self, num = 10):
        self.rand = np.random.randn(num)
        for n in self.rand:
            self.s = self.next(n)
            self.S.append(self.s)
        return self.S
    
    def debug(self):
        print "[DEBUG mu=%s sigma2=%s]"%(self.mu, self.sigma2)
        print "[DEBUG Random Sample]:"
        print self.rand
        print "[DEBUG Stock Price]:"
        print self.S

    def draw(self, fig=plt.figure()):
        plt.plot(self.S)
        plt.xlabel('Day')
        plt.ylabel('Price')
        plt.grid(True)
        plt.show()

model = lto(mu=0.05,sigma2=0.3,s=100.0,dt=1.0/365)
model.gen(num=365)
model.debug()
model.draw()
